package com.youdianstar.app.ui.web

import android.view.ViewGroup
import android.webkit.JavascriptInterface
import android.webkit.WebView
import android.widget.LinearLayout

import com.youdianstar.app.databinding.WebActivityBinding
import com.youdianstar.app.R
import com.youdianstar.app.BR
import com.youdianstar.app.common.Extra
import com.youdianstar.app.common.base.BaseActivity
import com.youdianstar.app.common.util.LogUtil
import com.youdianstar.app.common.util.WebCsSettings
import com.youdianstar.app.common.ex.setThrottleListener

import com.just.agentweb.AgentWeb
import com.gyf.immersionbar.ImmersionBar
import com.just.agentweb.DefaultWebClient


/**
 * File: WebActivity
 * Author: 82149 Create: 2021/12/10 14:25
 * Changes (from 2021/12/10)
 * --------------------------------------------------
 * description: web activity
 * ---------------------------------------------------
 *
 */
class WebActivity :BaseActivity<WebActivityBinding, WebViewModel>(){

    override fun layoutId(): Int = R.layout.web_activity

    override fun initVariableId(): Int = BR.viewModel

    override fun initViewModel(): WebViewModel {
        return createViewModel()
    }

    private lateinit var mAgentWeb: AgentWeb

    open inner class AndroidInterfaceForJS{
        @JavascriptInterface
        fun callAndroid(){
            finish()
        }
    }

    override fun initViewLayout() {
        super.initViewLayout()
        val title : String = intent.getStringExtra(Extra.IT_NOTIFY)?:""
        mBinding.headTitle.text = title
        mBinding.headBack.setThrottleListener {
            finish()
        }
        ImmersionBar.with(this)
            .statusBarColor(R.color.color_39404D)
            .statusBarDarkFont(false)
            .titleBarMarginTop(mBinding.headLlBar)
            .init()

        val url : String = intent.getStringExtra(Extra.IT_DATA)?:""
        mAgentWeb = AgentWeb.with(this)
            .setAgentWebParent(mBinding.container, LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT))
            .useDefaultIndicator()
            .setAgentWebWebSettings(WebCsSettings())
            .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
            .setMainFrameErrorView(com.just.agentweb.R.layout.agentweb_error_page, -1)
            .setOpenOtherPageWays(DefaultWebClient.OpenOtherPageWays.ASK) //打开其他应用时，弹窗咨询用户是否前往其他应用
            .setWebChromeClient(object :com.just.agentweb.WebChromeClient(){
                override fun onReceivedTitle(view: WebView?, title: String?) {
                    super.onReceivedTitle(view, title)
                }
            })
            .setWebViewClient(object :com.just.agentweb.WebViewClient(){
                override fun onPageFinished(view: WebView?, url: String?) {
                    LogUtil.d("onPageFinished", url)
                    super.onPageFinished(view, url)
//                    val successLink = "https://api.brezze.fr/payment/index.html#/"
//                    val failLink = "https://api.brezze.fr/payment/index.html#/defeated"
//                    url?.let {
//                        if (it.contains(failLink)){
//                            RxBus.getDefault().post(BaseEvent<PayPalState>(DataCode.EVENT_PAYPAL_CALLBACK,PayPalState(2,RegexUtils.getParamByUrl(it,"failureReason"))))
//                            finish()
//                        } else if (it.contains(successLink)){
//                            RxBus.getDefault().post(BaseEvent<PayPalState>(DataCode.EVENT_PAYPAL_CALLBACK,PayPalState(1)))
//                            finish()
//                        }
//                    }
                }
            })
            .interceptUnkownUrl() //拦截找不到相关页面的Scheme
            .createAgentWeb()
            .ready()
            .go(url)
    }

    override fun initClickEvent() {

    }

    override fun onPause() {
        mAgentWeb.webLifeCycle.onPause()
        super.onPause()
    }

    override fun onResume() {
        mAgentWeb.webLifeCycle.onResume()
        super.onResume()
    }

    override fun onDestroy() {
        mAgentWeb.webLifeCycle.onDestroy()
        super.onDestroy()
    }

    override fun onBackPressed() {
        if (mAgentWeb.back().not()) {
            super.onBackPressed()
            //hideSoftKeyboard(this)
        }
    }
}